class Admin::CategoriesController < Admin::AdminApplicationController
  layout "admin"
  before_filter :admin_login_required
  
  def index
    @categories = PostCategory.paginate :conditions => ["parent_id = 0"], :page => params[:page]
  end
  def show
    @category = PostCategory.find(:first, :conditions => ["id = ?", params[:id]])
    @children = @category.children
  end
  
  def new
    @category = PostCategory.new
  end
  
  def create
    @category = PostCategory.new(params[:category])
    if @category.save
      flash[:notice] = '��������ɹ�.'
      redirect_to categories_path
    else
      render new_categories_path
    end
  end
  
  def edit
    @category = PostCategory.find(:first, :conditions => ["id = ?", params[:id]])
  end
  
  def update 
    @category = PostCategory.find(:first, :conditions => ["id = ?", params[:id]])
    @category.update_attributes(params[:category])
    redirect_to admin_category_path(@category)
  end
  
  def destroy
    @category = PostCategory.find(:first, :conditions => ["id = ?", params[:id]])
    @category.destroy
    redirect_to admin_categories_path
  end
  
  def show_children
    @category = PostCategory.find(:first, :conditions => ["id = ?", params[:id]])
    @children = @cat.children
  end
  def new_child
    @parent = PostCategory.find(:first, :conditions => ["id = ?", params[:id]])
    @children_category = PostCategory.new
  end
  
  def create_child
    @parent = PostCategory.find(:first, :conditions => ["id = ?", params[:parent][:id]])
    @parent.children.create(params[:category])
    redirect_to admin_categories_path
  end
end
